package com.neu.af.util;

import com.neu.af.dataFilter.entity.AbData;
import com.neu.af.dataFilter.entity.Data;
import com.neu.af.dataFilter.mapper.TagMapper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

public class DataUtil {

    public static List<String> readStringDataFromTxt(String path) {

        ArrayList<String> strings = new ArrayList<>();
        BufferedReader bufferedReader = null;
        try {
//            InputStream inputStream = DataUtil.class.getClassLoader().getResourceAsStream(path);
            File txtFile = new File(path);
//            // 使用common-io的工具类即可转换
//            FileUtils.copyInputStreamToFile(inputStream,txtFile);

//            File file = ResourceUtils.getFile("classpath:"+path);
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(txtFile), "utf-8"));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                // System.out.println(line);
                strings.add(line);

            }
            bufferedReader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        return strings;
    }

    public static List<Data> string2DataList(List<String> strings) {
        ArrayList<Data> data = new ArrayList<>();
        for(String s: strings) {
            Data d = new Data();
            String[] ss = s.split(" ");
            d.setTag_name(ss[0]);
            d.setCreate_time(Timestamp.valueOf(ss[1].replace("/","-")+" "+ss[2]));
            d.setData(ss[3]);
            data.add(d);
        }
        return data;
    }

    public static List<Data> filterByTagName(List<Data> data, String tagName) {
        return data.stream().filter(data1 -> data1.getTag_name().equals(tagName)).collect(Collectors.toList());
    }

    public static List<Data> filterDataNullWithTime(List<Data> data, String starttime, String endtime) {
        return data.stream().filter(data1 -> data1.getCreate_time().before(Timestamp.valueOf(endtime))
                && data1.getCreate_time().after(Timestamp.valueOf(starttime))
                && data1.getData().equals("System.__ComObject")
        ).collect(Collectors.toList());
    }

    public static List<Data> filterDataNull(List<Data> data) {
        return data.stream().filter(data1 -> data1.getData().equals("System.__ComObject")
        ).collect(Collectors.toList());
    }

    public static List<Data> filterDataNullTagName(List<Data> data, String tagName, String starttime, String endtime) {
        return data.stream().filter(data1 -> data1.getTag_name().equals(tagName)
                && data1.getCreate_time().before(Timestamp.valueOf(endtime))
                && data1.getCreate_time().after(Timestamp.valueOf(starttime))
                && data1.getData().equals("System.__ComObject")
        ).collect(Collectors.toList());
    }

    public static List<Data> filterDataNotNull(List<Data> data, String tagName, String starttime, String endtime) {
        return data.stream().filter(data1 -> data1.getTag_name().equals(tagName)
                && data1.getCreate_time().before(Timestamp.valueOf(endtime))
                && data1.getCreate_time().after(Timestamp.valueOf(starttime))
                && !data1.getData().equals("System.__ComObject")
        ).collect(Collectors.toList());
    }

    public static Timestamp getMinCreateTime(List<AbData> abdata) {
        if(abdata==null || abdata.size()==0) {
            return null;
        }
        return abdata.stream().min(Comparator.comparing(AbData::getCreate_time)).get().getCreate_time();
    }

    public static Timestamp getMinCreateTimeD(List<Data> data) {
        if(data==null || data.size()==0) {
            return null;
        }
        return data.stream().min(Comparator.comparing(Data::getCreate_time)).get().getCreate_time();
    }

    public static List<Data> piString2Data(String s) {
        ArrayList<Data> dataList = new ArrayList<>();
        String[] splitByBSTP = s.split("BSTP");
        System.out.println(splitByBSTP);
        for(int i = 1; i < splitByBSTP.length; i ++) {
            String ss = splitByBSTP[i];
            String[] ssList = ss.split(" ");
            // ss: 011ASLOP 2021/4/30 18:57:17 0
            Data data = new Data();
            String tagName = "";
            for(int ii = 0; ii < ssList.length; ii ++) {
                // System.out.println(ssList[ii]);
                if(ssList[ii].startsWith("20")) {
                    String createTimeString = ssList[ii].replace("/", "-")+" "+ssList[ii+1];
                    // System.out.println(createTimeString);
                    data.setCreate_time(Timestamp.valueOf(createTimeString));
                    data.setData(ssList[ii+2]);
                    break;
                }
                else {
                    // System.out.println(111);
                    tagName=tagName+ssList[ii];
                    // System.out.println("tttttttt "+tagName);
                }
            }
            data.setTag_name("BSTP"+tagName);
            dataList.add(data);
        }
        return dataList;
    }

    public static List<Data> piString2Data2(String s) {
        // System.out.println(s);
        ArrayList<Data> dataList = new ArrayList<>();
        String[] splitByBSTP = s.split("BSTP");
        // System.out.println(splitByBSTP);
        for(int i = 1; i < splitByBSTP.length; i ++) {
            String ss = splitByBSTP[i];
            String[] ssList = ss.split(" ");
            // ss: 011ASLOP 2021/4/30 18:57:17 0
            if (ssList[ssList.length-1].equals("0")) continue; // 注意这样有可能最后一条是0，因为最后一条带了总数
            Data data = new Data();
            String tagName = "";
            for(int ii = 0; ii < ssList.length; ii ++) {
                // System.out.println(ssList[ii]);
                if(ssList[ii].startsWith("20")) {
                    String createTimeString = ssList[ii].replace("/", "-")+" "+ssList[ii+1];
                    // System.out.println(createTimeString);
                    data.setCreate_time(Timestamp.valueOf(createTimeString));
                    data.setData(ssList[ii+2]);
                    break;
                }
                else {
                    // System.out.println(111);
                    tagName=tagName+ssList[ii];
                    // System.out.println("tttttttt "+tagName);
                }
            }
            data.setTag_name("BSTP"+tagName);
            dataList.add(data);
        }
        return dataList;
    }

    public static void main(String[] args) {
        // List<Data> data = DataUtil.piString2Data("BSTP:011ASLOP 2021/4/30 18:57:17 0 BSTP:011ASLOP 2021/5/1 2:47:39 0 BSTP:011ASLOP 2021/5/1 10:38:05 0 BSTP:011ASLOP 2021/5/1 14:18:23 0 4");
        //System.out.println(DataUtil.initsList());
    }
}
